package com.tjsinfo.tjsoa.meetingroom.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.LogicalExpression;
import org.hibernate.criterion.Order;

import com.tjsinfo.tjsoa.Dao.IQbcDao;
import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.meetingroom.vo.MeetingIncept;
import com.tjsinfo.tjsoa.meetingroom.vo.MeetingRoomEmploy;
import com.tjsinfo.tjsoa.system.vo.User;

public class HuiYiAdminAction extends Action{
	
	private IQbcDao qbcDao; 
	
	public IQbcDao getQbcDao() {
		return qbcDao;
	}

	public void setQbcDao(IQbcDao qbcDao) {
		this.qbcDao = qbcDao;
	}


	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
	    User user = (User)session.getAttribute("user");
	    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    //所申请会议
	    DetachedCriteria criteria1 = DetachedCriteria.forClass(MeetingRoomEmploy.class);
	    criteria1.add(Expression.eq("applicationUserId", Integer.valueOf(user.getUser_id()).toString()));
	    //criteria1.add(Expression.eq("applicationUserName", user.getUsername()));
	    criteria1.add(Expression.ge("meetingTime",df.format(new Date().getTime()-24*60*60*1000)));
	    ResourceBundle bundle1 = ResourceBundle.getBundle("com.tjsinfo.tjsoa.meetingroom.properties.systemResource");
	    Integer pageSize1 = Integer.valueOf(bundle1.getString("empolyList.jsp.pageSize1"));
	    String pageStr1 = request.getParameter("pageNo1");
	    if (pageStr1 == null) pageStr1 = "0";
	    int page1 = Integer.valueOf(pageStr1).intValue();
	    int startIndex1 = pageSize1.intValue() * page1;
	    List orderList1 = new ArrayList();
	    orderList1.add(Order.desc("empolyTime"));

	    PaginationSupport paginationSupport1 = this.qbcDao.findPageByCriteria(criteria1, orderList1, Integer.valueOf(pageSize1.intValue()).intValue(), startIndex1);
	    session.setAttribute("myMeetingList1", paginationSupport1);

	    Calendar cal=Calendar.getInstance();
        cal.add(Calendar.DATE,-1);
        Date time=cal.getTime();
        Integer state1=0;
	    DetachedCriteria criteria2 = DetachedCriteria.forClass(MeetingIncept.class);
	    criteria2.add(Expression.eq("receiveUserId", Integer.valueOf(user.getUser_id())));
	    criteria2.add(Expression.eq("state",state1));
	    criteria2.add(Expression.ge("startTime",time));//>= Restrictions.ge() 大于等于
	    ResourceBundle bundle2 = ResourceBundle.getBundle("com.tjsinfo.tjsoa.meetingroom.properties.systemResource");
	    Integer pageSize2 = Integer.valueOf(bundle2.getString("empolyList.jsp.pageSize1"));
	    String pageStr2 = request.getParameter("pageNo2");
	    if (pageStr2 == null) pageStr2 = "0";
	    int page2 = Integer.valueOf(pageStr2).intValue();
	    int startIndex2 = pageSize2.intValue() * page2;
	    List orderList2 = new ArrayList();
	    orderList2.add(Order.desc("startTime"));
	    PaginationSupport paginationSupport2 = this.qbcDao.findPageByCriteria(criteria2, orderList2, Integer.valueOf(pageSize2.intValue()).intValue(), startIndex2);
	    request.setAttribute("myMeetingList2", paginationSupport2);

	    String userId = "," + String.valueOf(user.getUser_id()) + ",";
	    DetachedCriteria criteria = DetachedCriteria.forClass(MeetingRoomEmploy.class);
	    LogicalExpression a = Expression.and(Expression.eq("examineUserId", userId), Expression.eq("state", "3"));
	    LogicalExpression b = Expression.and(a,Expression.ge("meetingTime",df.format(new Date().getTime()-24*60*60*1000)));
	    criteria.addOrder(Order.asc("id"));
	    criteria.add(b);
	    TreeMap TM = new TreeMap();
	    List roomList = new ArrayList();
	    List list = this.qbcDao.getCriteria(criteria);
	    for (Iterator iterator = list.iterator(); iterator.hasNext(); ) {
	      MeetingRoomEmploy employ = (MeetingRoomEmploy)iterator.next();
	      if (TM.get(employ.getMeetingRoomMain().getId()) == null) {
	        TM.put(employ.getMeetingRoomMain().getId(), employ);
	      }
	    }
	    if (TM.size() > 0) {
	      Set<String> set = TM.keySet();
	      for (String s : set) {
	        roomList.add((MeetingRoomEmploy)TM.get(s));
	      }
	    }
	    request.setAttribute("roomList", roomList);

	    return mapping.findForward("ok");
	  }

}
